const {sequelize,Sequelize} = require('../../database/init')
const { Op } = require("sequelize");
const {QuestionBase,Question} =  require('../dbModel/QuestionBase')
const {User} = require('../dbModel/User')
const {Organization,Member} = require('../dbModel/Organizetion')
const {TestPaper,TestQuestion}= require('../dbModel/Paper')
const {Examination,ExamPaper,ExamQuestion,AnswerPaper,AnswerQuestion}=require('../dbModel/Examination')

{
    /*
* 在QuestionBase上创建外键creatorid:表示题库的创建者id
一(user)对多(QuestionBase)关系
一(用户)对多(题库)关系
* */
    User.hasMany(QuestionBase,{foreignKey:'creatorid'});
    QuestionBase.belongsTo(User,{foreignKey:'creatorid'});


    /*
    * 在Question上创建外键QuestionBaseid:表示问题所属的题库
    *一(QuestionBase)对多(Question)关系
    * 一(题库)对多(问题)关系
    * */
    QuestionBase.hasMany(Question,{foreignKey:'QBID'});
    Question.belongsTo(QuestionBase,{foreignKey:'QBID'});

    /*
    *在Question上创建外键creatorid:表示问题的创建者
    * 一(User)对多(Question)
    * 关系一(用户)对多(题目)关系
    * */
    User.hasMany(Question,{foreignKey:'creatorid'})
    Question.belongsTo(User, {foreignKey: 'creatorid'})

    /*
    *通过Member连接表，关联组织Organization和用户User(成员)
    * */
    User.belongsToMany(Organization,{through:Member,foreignKey:'user_id',otherKey:'org_id'})
    Organization.belongsToMany(User,{through:Member,foreignKey:'org_id',otherKey:'user_id'})
    /*
    * 在Organization上创建外键creatorid:表示组织的创建者
    * 一(User)对多(Organization)关系
    * 一(用户)对多(组织)关系
    * */
    Organization.belongsTo(User,{foreignKey:'creator_id',as:'creator'})
    User.hasMany(Organization,{foreignKey:'creator_id',as:'creator'})
    /*
    * 在TestQuestion连接表，关联试卷和题目以获得试题：
    * */
    Question.belongsToMany(TestPaper,{through:TestQuestion,foreignKey:'question_id',otherKey:'test_paper_id'})
    TestPaper.belongsToMany(Question,{through:TestQuestion,foreignKey:'test_paper_id',otherKey:'question_id'})
    /*
    *在试卷上创建外键，表示试卷的创建者
    * 多(TestPaper)对一(User)关系
    * 多(试卷)对一(用户)关系
    * */
    TestPaper.belongsTo(User,{foreignKey:'creator_id'});
    User.hasMany(TestPaper,{foreignKey:'creator_id'})
    /*
    * 在考试上建立外键:表示考试所使用的的考卷
    * 一(Examination)对一(ExamPaper)关系
    * 一(考试)对一(考卷)关系
    * */
    Examination.belongsTo(ExamPaper,{foreignKey:'exam_paper_id'});
    ExamPaper.hasOne(Examination,{foreignKey:'exam_paper_id'});
    /*
    *在考试上建立外键：表示考试所属于的组织
    * 一(Organization)对多(Examination)关系
    * 一(组织)对多(考试)关系
    * */
    Examination.belongsTo(Organization,{foreignKey:'org_id'})
    Organization.hasMany(Examination,{foreignKey:'org_id'})
    /*
    *在考题上建立外键，表示考题所属于的考卷
    * 多(ExamQuestion)对一(ExamPaper)关系
    * 多(考题)对一(考卷)关系
    * */
    ExamQuestion.belongsTo(ExamPaper,{foreignKey:'exam_paper_id'});
    ExamPaper.hasMany(ExamQuestion,{foreignKey:'exam_paper_id'})
    /*
    * 在考题上建立外键,表示考题所对应题库中的原题
    * 多(ExamQuestion)对一(Question)关系
    * 多(考题)对一(问题||原题)关系
    * */
    ExamQuestion.belongsTo(Question,{foreignKey:'question_id'});
    Question.hasMany(ExamQuestion,{foreignKey:'question_id'});
    /*
    *在答题上建立外键，表示答题所属于的答卷
    * 一(AnswerPaper)对多(AnswerQuestion)关系
    * 一(答卷)对多(答题)关系
    * */
    AnswerPaper.hasMany(AnswerQuestion,{foreignKey:'answer_paper_id'})
    AnswerQuestion.belongsTo(AnswerPaper,{foreignKey:'answer_paper_id'});
    /*
    * 在答卷上建立外键，表示答卷所属于的考卷
    * 一(ExamPaper)对多(AnswerPaper)关系
    * 一(考卷)对多(答卷)关系
    * */
    AnswerPaper.belongsTo(ExamPaper,{foreignKey:'exam_paper_id'})
    ExamPaper.hasMany(AnswerPaper,{foreignKey:'exam_paper_id'})
    /*
    * 在答卷上建立外键，表示答卷所属于的用户
    * 一(User)对多(AnswerPaper)关系
    * 一(用户)对多(答卷)关系
    * */
    AnswerPaper.belongsTo(User,{foreignKey:'user_id'})
    User.hasMany(AnswerPaper,{foreignKey:'user_id'})
    /*
    * 在答题上建立外键，表示答题所对应的考题，
    * 一(ExamQuestion)对多(AnswerQuestion)关系
    * 一(考题)对多(答题)关系
    * */
    ExamQuestion.hasMany(AnswerQuestion,{foreignKey:'exam_question_id'})
    AnswerQuestion.belongsTo(ExamQuestion,{foreignKey:'exam_question_id'})



}
sequelize.sync().then(
    //r => console.log('所有模型均已成功同步.')
);

//删除所有表
//sequelize.drop();

// async function test(){
// }
//
// test();

module.exports = {
    sequelize,
    Sequelize,
    Op,
    QuestionBase,
    Question,
    User,
    Organization,
    Member,
    TestPaper,
    TestQuestion,
    Examination,
    ExamPaper,
    ExamQuestion,
    AnswerPaper,
    AnswerQuestion,
}
